﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class Provider_CarInsurance_ProviderCarNCDProtection : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCINCDPRTC";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            GetCoverType();
        }

    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
             string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
             if (CheckAuthCode(_authcode, 100))
             {
                 CarNCD _CarNCD = new CarNCD();
                 CarNCDDB _CarNCDdb = new CarNCDDB();

                 _CarNCD.ProviderNCDId = CheckProviderCarNCD(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt64(ddlCoverType.Text));
                 int _trantype = 1;
                 if (_CarNCD.ProviderNCDId > 0)
                 {
                     _trantype = 2;
                 }
                 _CarNCD.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
                 _CarNCD.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());

                 _CarNCD.NCD = "";
                 _CarNCD.Rate = Convert.ToSingle(txtNCD.Text);
                 _CarNCD.NInfo1 = Convert.ToInt64(ddlCoverType.SelectedValue);
                 _CarNCD.Status = true;
                 _CarNCD.FInfo1 = 0;
                 _CarNCD.DInfo1 = CE.Global.General.GetUTCDateTime();

                 _CarNCD.ProviderNCDId = _CarNCDdb.Insert(_CarNCD);
                 if (_CarNCD.ProviderNCDId > 0)
                 {
                     InsertProviderAuthTransaction(_CarNCD.ProviderNCDId, "CICarNCD", _trantype, 100, _authcode, _CarNCD.ProviderCompanyId, _CarNCD.RefCEUserId);
                     lblNCDMessage.Text = "Updated successfully.";
                     //Response.Redirect("ViewProviderCarNCD.aspx");
                 }
                 else
                 {
                     lblNCDMessage.Text = "Updating failed.";
                 }
             }
             else
             {
                 lblNCDMessage.Text = "Wrong auth code!";
             }
        }
    }

    protected void GetCoverType()
    {
        CarCoverTypeList _cctypelist = new CarCoverTypeList();
        CarCoverTypeDB _cctypedb = new CarCoverTypeDB();

        _cctypelist = _cctypedb.GetProviderCoverTypeList(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]));

        if (_cctypelist != null)
        {
            for (int i = 0; i < _cctypelist.Count; i++)
            {
                ddlCoverType.Items.Add(new ListItem(_cctypelist[i].ProviderCoverTypeDescription.ToString(), _cctypelist[i].CXCoverTypeId.ToString()));
            }
        }

        ddlCoverType.Items.Insert(0, new ListItem("Select", "0"));

    }

    protected Int64 CheckProviderCarNCD(Int64 ProviderCompanyId, Int64 CoverType)
    {
        CarNCD _CarNCD = new CarNCD();
        CarNCDDB _CarNCDdb = new CarNCDDB();
        Int64 _retValue = 0;
        _CarNCD = _CarNCDdb.GetProviderCarNCD(ProviderCompanyId, CoverType);
        if (_CarNCD != null)
        {
            _retValue = _CarNCD.ProviderNCDId;
        }
        return _retValue;
    }

    protected void ShowCarNCD(Int64 ProviderCompanyId, Int64 CoverType)
    {
        CarNCD _CarNCD = new CarNCD();
        CarNCDDB _CarNCDdb = new CarNCDDB();

        _CarNCD = _CarNCDdb.GetProviderCarNCD(ProviderCompanyId, CoverType);
        if (_CarNCD != null)
        {
            ddlCoverType.Text = _CarNCD.NInfo1.ToString();
            txtNCD.Text = _CarNCD.Rate.ToString();
        }
        else
        {
            txtNCD.Text = "";
        }
        lblNCDMessage.Text = "";
    }

    protected void ddlCoverType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowCarNCD(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt64(ddlCoverType.Text));
    }


    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/CarInsurance/TableMatching.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
        }
    }

}
